//
//  Login.swift
//  jiweiapp
//
//  Created by Kin Wong on 15/8/11.
//  Copyright (c) 2015年 Kin Wong. All rights reserved.
//

import UIKit

class Login: UIViewController, UITextFieldDelegate {

    //创建变量
    var accountLabel = UILabel() //账户标签
    var accountText = UITextField() //账户输入框
    var passwordLabel = UILabel() //密码标签
    var passwordText = UITextField() //密码输入框
    var login: UIButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton //登陆按钮
    
    override func viewDidLoad() {
        super.viewDidLoad() //调用父类构造函数
        
        accountText.frame = CGRectMake(50, CGRectGetHeight(self.view.bounds)*(3/5), CGRectGetWidth(self.view.bounds)-100, 50)
        passwordText.frame = CGRectMake(50, CGRectGetHeight(self.view.bounds)*(3/5)+50, CGRectGetWidth(self.view.bounds)-100, 50)
        login.frame = CGRectMake(98, CGRectGetHeight(self.view.bounds)*(3/5)+100, CGRectGetWidth(self.view.bounds)-200, 50)
        login.setTitle("登陆", forState: UIControlState.Normal) //登陆按钮样式
        accountText.borderStyle = UITextBorderStyle.None //输入框样式
        accountText.placeholder = "请输入账户名" //输入框提示语句
        accountText.clearButtonMode = UITextFieldViewMode.WhileEditing //输入时输入框出现清除按钮
        accountText.returnKeyType = UIReturnKeyType.Done //设置键盘的Return键
        accountText.delegate = self //设置代理（相当于监听）
        passwordText.borderStyle = UITextBorderStyle.None
        passwordText.placeholder = "请输入密码"
        passwordText.clearButtonMode = UITextFieldViewMode.WhileEditing
        passwordText.secureTextEntry = true
        passwordText.returnKeyType = UIReturnKeyType.Done
        passwordText.delegate = self
        login.addTarget(self, action: Selector("jumpToMain"), forControlEvents: UIControlEvents.TouchDown) //加入登陆按钮事件
        
        self.view.addSubview(accountLabel)
        self.view.addSubview(passwordLabel)
        self.view.addSubview(login)
        self.view.addSubview(accountText)
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background", inBundle: NSBundle(forClass: Login.self), compatibleWithTraitCollection: nil)!)
        self.view.addSubview(passwordText) //显示各部件到屏幕上
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        accountText.resignFirstResponder()
        passwordText.resignFirstResponder()
    } //触摸输入框后弹出键盘
    
    func textFieldDidBeginEditing(TextField: UITextField) {
        let frame: CGRect = TextField.frame
        var offset: CGFloat
        let animationDuration : NSTimeInterval = 0.30
        offset = frame.origin.y+96-(self.view.frame.size.height-216)
        UIView.beginAnimations("ResizeForKeyboard", context: nil)
        UIView.setAnimationDuration(animationDuration)
        if(offset > 0) {
            self.view.frame = CGRectMake(0.0, -offset, self.view.frame.size.width,
                self.view.frame.size.height)
        }
        
        UIView.commitAnimations()
    } //如果键盘挡住输入框，则把显示画面向上移动offset个单位
    
    func textFieldDidEndEditing(TextField: UITextField) {
        self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
    } //输入完成后恢复画面位置
    
    func textFieldShouldReturn(TextField: UITextField) -> Bool {
        TextField.resignFirstResponder()
        return true
    } //输入完成后收回键盘
    
    func jumpToMain() {
        if (accountText.text.isEmpty) || (passwordText.text.isEmpty) {
            var alertView = UIAlertView()
            alertView.title = "错误"
            alertView.message = "请输入账户和密码"
            alertView.addButtonWithTitle("好")
            alertView.show()
        }
        else {
            var main = MainTabView()
            main.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
            self.dismissViewControllerAnimated(true, completion: nil)
            self.presentViewController(main, animated: true, completion: nil)
        }
    } //跳转到主界面

}
